package qiurank;

import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/1/8.
 */
public class PaFather extends Object{
    protected String url;
    protected String htmlContent;
    protected URL urlobj;
    protected UrlManageFather urlManage;
    //public SameWordModel sameWordModel;
    protected UrlObject urlObject;
    protected PaFather(UrlObject _urlObject, UrlManageFather _urlManage){
        urlManage=_urlManage;
        urlObject=_urlObject;
        //sameWordModel=new SameWordModel();
        url=urlObject.url;
        try {
            urlobj=new URL(url);
        }catch (Exception e){

        }

        htmlContent=Http.get(url).replaceAll("\r","").replaceAll("\n","").replaceAll(" {2,}","");
    }
    public UrlObject getUrlObject(){
        return urlObject;
    }
    public String getUrl(){
        return url;
    }
    public UrlManageFather getUrlManage(){
        return urlManage;
    }
    public String getHtmlContent(){
       return getHtmlContent(null);
    }
    public String getHtmlContent(String _content){
        //规则
        String pat="<jphtml>(.*?)</jphtml>";
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=null;
        if(_content!=null){
            matcher=pattern.matcher(_content);
        }else {
            matcher=pattern.matcher(htmlContent);
        }
        if(matcher.find()){
            return matcher.group(1);
        }

        return null;
    }
    public String getRawHtmlContent(){
        return htmlContent;
    }
    public String getContentType(){
        //规则
        String pat="<Content-Type>(.*?)</Content-Type>";
        //执行
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);

        if(matcher.find()){
            return matcher.group(1);
        }

        return null;
    }
    public String getBodyContent(){
        //规则
        String pat="<body>(.*?)</body>";
        //执行
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);

        if(matcher.find()){
            return matcher.group(1);
        }

        return null;
    }
    /**
     * 获得页面的标题 title
     * @return
     */
    public String getPageTitle(){
        //规则
        String pat="<title>(.*?)</title>";
        //执行
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);
        if(matcher.find()){
            return matcher.group(1);
        }

        return null;
    }
    protected void saveUrl(String _url){
        urlManage.addUrl(_url,0);
    }
    protected void saveUrl(String _url,int weight){
        urlManage.addUrl(_url,weight);
    }
}
